package admin.sys;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.CommonUtil;
import com.common.MD5;
import com.common.ResultData;
import com.filter.BaseController;
import com.intercepter.AuthPassport;

import beans.sys.UserBean;
import beans.sys.UserModel;
import service.sys.UserServiceImpl;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController{

	@Resource
	private UserServiceImpl userServiceImpl;

	@RequestMapping("/index")
	public String Index(){
		return "admin/sys/user/index";
	}

	@RequestMapping("/GetPageListJson")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="user")
	public void DataGrid(UserModel model,String condition,String keyword){
		ResultData<List<Map>> rd = new ResultData<>();
		try{
			rd.setPage(model.getPage());
			model.setType("1");
			if(!CommonUtil.isNullObject(condition)) {
				if(condition.equals("account"))model.setAccount(keyword);
				else if(condition.equals("username"))model.setUsername(keyword);
			}
			List<Map> m = userServiceImpl.queryByList(model);
			rd.setRows(m);
			rd.setRecords(model.getPager().getRowCount());
			rd.setTotal(model.getPager().getPageCount());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		responseJson(rd);
	}

	@RequestMapping("/detail")
	@AuthPassport(actionName="index",controllerName="user")
	public String Detail(String id){
		UserBean m = new UserBean();
		m.setIsuse("1");
		if(!CommonUtil.isNullObject(id)){
			try {
				m = (UserBean) userServiceImpl.queryById(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		getReq().setAttribute("user", m);
		return "admin/sys/user/detail";
	}

	@RequestMapping("/save")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="user")
	public void Save(UserBean ub){
		ResultData<String> rd = new ResultData<>();
		try {
			UserBean user = userServiceImpl.findByAccount(ub.getId(), ub.getAccount());
			if(user!=null){
				rd.setMessage("当前工号已经存在!");
				responseJson(rd);
				return;
			}

			if(CommonUtil.isNullObject(ub.getId())){//新增
				UserBean u = (UserBean)getReq().getSession().getAttribute("user");

				ub.setId(UUID.randomUUID().toString());
				ub.setAddtime(new Date());
				ub.setPassword(MD5.MD5Encode(ub.getPassword()));
				ub.setAdduser(u.getId());
				ub.setIsuse("1");
				userServiceImpl.add(ub);
			}
			else{//编辑
				userServiceImpl.update(ub);
			}
			rd.setSuccess(true);
		} catch (Exception e) {
			rd.setType(3);
			rd.setMessage("系统错误,请联系管理员!");
			e.printStackTrace();
		}
		rd.setType(1);
		rd.setMessage("保存成功!");
		responseJson(rd);
	}

	@RequestMapping("/ExistAccount")
	@ResponseBody
	public void ExistAccount(String account,String keyValue) {
		UserBean user = userServiceImpl.findByAccount(keyValue, account);
		if(user!=null){
			responseText("false");
		}
		else
			responseText("true");
	}

	@RequestMapping("/del")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="user")
	public void Del(){
		String[] ids = getReq().getParameterValues("ids");
		try{
			userServiceImpl.deletes(ids);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setType(1);
		rd.setMessage("删除成功");
		responseJson(rd);
	}

	@RequestMapping("/editpwd")
	@ResponseBody
	public void EditPwd(String id){
		try{
			userServiceImpl.initPwd(id, MD5.MD5Encode("11111"));
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setType(1);
		rd.setMessage("初始化成功");
		responseJson(rd);
	}

	@RequestMapping("/EnabledAccount")
	@ResponseBody
	@AuthPassport(actionName="index",controllerName="user")
	public void EnabledAccount(String id,String type){
		try{
			userServiceImpl.UseStatus(id, type);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		ResultData<String> rd = new ResultData<>();
		rd.setSuccess(true);
		rd.setMessage("状态更改成功");
		responseJson(rd);
	}

	@RequestMapping("/editpwdindex")
	public String EditPwdIndex() {
		return "admin/sys/user/editpwdindex";
	}

	@RequestMapping("/editpwd2")
	@ResponseBody
	public void EditPwd2(String old,String newp){
		ResultData<String> rd = new ResultData<>();
		try{
			UserBean u = (UserBean)getReq().getSession().getAttribute("user");
			UserBean m = (UserBean) userServiceImpl.queryById(u.getId());
			if(MD5.MD5Encode(old).equals(m.getPassword())){
				userServiceImpl.initPwd(m.getId(), MD5.MD5Encode(newp));
				u.setPassword(MD5.MD5Encode(newp));
				rd.setType(1);
				rd.setMessage("保存成功");
			}
			else{
				rd.setMessage("旧密码错误!");
			}
		}
		catch(Exception e){
			e.printStackTrace();
			rd.setType(2);
		}
		responseJson(rd);
	}
}
